using System.Linq;
using Volo.Abp.Localization;
using Volo.Abp.Authorization.Permissions;
using Joy.Erp.BasicArchives.Localization;

namespace Joy.Erp.BasicArchives.Permissions
{
    public class EquipmentPermissionDefinitionProvider : PermissionDefinitionProvider
    {
        public override void Define(IPermissionDefinitionContext context)
        {
            var menus = context.GetGroupOrNull(BasicArchivesPermissions.Menus);
            if (menus == null)
            {
                menus = context.AddGroup(BasicArchivesPermissions.Menus, L("Permission:Menus"));
            }

            var basicArchivesMenu = menus.GetPermissionOrNull(BasicArchivesPermissions.BasicArchivesMenu.Default);
            if (basicArchivesMenu == null)
            {
                basicArchivesMenu = menus.AddPermission(BasicArchivesPermissions.BasicArchivesMenu.Default, L("Permission:Menus.BasicArchives.ArchivesManagement"));
            }

            var parentMenu = basicArchivesMenu.Children.FirstOrDefault(x => x.Name == BasicArchivesPermissions.Equipment.ParentMenu);
            if (parentMenu == null)
            {
                parentMenu = basicArchivesMenu.AddChild(BasicArchivesPermissions.Equipment.ParentMenu, L("Permission:Menus.BasicArchives.ArchivesManagement.ProductionUnit"));
            }

            var menu = parentMenu.Children.FirstOrDefault(x => x.Name == BasicArchivesPermissions.Equipment.Menu);
            if (menu == null)
            {
                menu = parentMenu.AddChild(BasicArchivesPermissions.Equipment.Menu, L("Permission:Menus.BasicArchives.ArchivesManagement.ProductionUnit.Equipment"));
            }

            var group = context.GetGroupOrNull(BasicArchivesPermissions.GroupName);
            if (group == null)
            {
                group = context.AddGroup(BasicArchivesPermissions.GroupName, L("Permission:BasicArchives"));
            }

            var pEquipmentDefault = group.AddPermission(BasicArchivesPermissions.Equipment.Default, L("Permission:BasicArchives.ProductionUnit.Equipment"));
            var pEquipmentQuery = pEquipmentDefault.AddChild(BasicArchivesPermissions.Equipment.Query, L("Permission:Query"));
            var pEquipmentEdit = pEquipmentDefault.AddChild(BasicArchivesPermissions.Equipment.Edit, L("Permission:Edit"));
            var pCreate = pEquipmentEdit.AddChild(BasicArchivesPermissions.Equipment.Create, L("Permission:Create"));
            var pUpdate = pEquipmentEdit.AddChild(BasicArchivesPermissions.Equipment.Update, L("Permission:Update"));
            var pDelete = pEquipmentEdit.AddChild(BasicArchivesPermissions.Equipment.Delete, L("Permission:Delete"));
        }

        private static LocalizableString L(string name)
        {
            return LocalizableString.Create<BasicArchivesResource>(name);
        }
    }
} 